package com.reggie.controller;

import com.reggie.Util.ValidateCodeUtils;
import com.reggie.common.R;
import com.reggie.Util.QQSMSUtils;
import com.reggie.pojo.User;
import com.reggie.service.impl.UserServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody String phone, HttpServletRequest request) {
        try {
            //这里的电话号就是邮箱的意思，懒得改了。
            System.out.println(phone);
            String str = phone.split(":")[1];
            int index = str.lastIndexOf("\"");
            String email = str.substring(1,index);
            //获取一个随机的验证码
            Integer integer = ValidateCodeUtils.generateValidateCode(4);
            String code = integer.toString();
            System.out.println(code);
            //将这个用户的邮箱和这个用户的验证码存入session中，用于数据的共享
            request.getSession().setAttribute("email", email);
            request.getSession().setAttribute("code", code);
            request.getSession().setMaxInactiveInterval(60 * 5);
            //发送邮件信息
            //QQSMSUtils.testMail(email, code);
            return R.success("发送成功！请注意查收");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/login")
    public R<String> login(@RequestBody String phone, HttpServletRequest request) {
        //把里面的数据取出来
        String[] split = phone.split(",");
        List<String> list = new ArrayList<>();
        for (String s : split) {
            list.add(s.split(":")[1]);
        }
        //数据处理
        String fist = list.get(0);
        int index1 = fist.lastIndexOf("\"");
        list.set(0, fist.substring(1, index1));
        String second = list.get(1);
        int index2 = second.lastIndexOf("\"");
        list.set(1, second.substring(1, index2));
        System.out.println(list);
        String email = (String) request.getSession().getAttribute("email");
        String code = (String) request.getSession().getAttribute("code");
        if (list.get(0).equals(email) && list.get(1).equals(code)) {
            //查看一下这个用户在表中存不存在，如果存在就登录不存在就添加到表中（注册）
            User user = userService.selectByEmail(list.get(0));
            if (user == null) {
                //注册
                User user_now = new User();
                user_now.setEmail(list.get(0));
                userService.add(user_now);
                request.getSession().setAttribute("user",list.get(0));
                return R.success("登录成功！");
            }else {
                //不为空,将这个对象的id存入session中，并返回
                request.getSession().setAttribute("user", user.getId());
                return R.success("登录成功！");
            }
        } else {
            return R.error("登录失败！验证码不正确");
        }
    }

    @PostMapping("/loginout")
    public R<String> loginOut(HttpServletRequest request){
        //出去掉session中存储的用户id
        request.getSession().removeAttribute("user");
        return R.success("已退出");
    }
}

